import { Canvas, Vec2, Node, Vec3, v3, UITransform } from "cc"
/**
 *  屏幕转空间坐标 (采用 canvas 转换)
 * @param canvas 相对节点上的画布
 * @param touchPos 屏幕触点，EventTouch 对象的 getLocation()，getStartLocation() 等
 * @param baseNode 参考的节点
 */
export function calculateScreenPosToSpacePosByCanvas(canvas: Canvas, touchPos: Vec2, baseNode: Node): Vec3 {
    const uiCamera = canvas.cameraComponent?.camera!;
    const worldPos = v3(0, 0, 0);
    uiCamera.screenToWorld(worldPos, v3(touchPos.x, touchPos.y, 0));
    return baseNode.getComponent(UITransform)!.convertToNodeSpaceAR(worldPos)
}

/**
 * 屏幕转空间坐标
 * @param targetPos 转化的目标节点
 * @param space 转到此节点的坐标空间
 */
export function calculateScreenPosToSpacePosByPos(targetPos: Vec2, space: Node): Vec3 {
    let worldPos: Vec3 = v3(targetPos.x, targetPos.y);
    let mapPos: Vec3 = space.getComponent(UITransform)!.convertToNodeSpaceAR(worldPos);
    return mapPos;
}